import { Condition } from './../domain/models/condition';
import { BaseService } from "./baseService";
import { Path } from "../domain/models/path";
import { ApiDataContext } from "../domain/apiDataContext";
import { Transaction } from "tiny-entity2";

export class PathService extends BaseService {
    CreatePath(entity: Path, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new Path();
            d.ConverToEntity(entity);
            return ctx.Create(d);
        });
    }
    CreatePaths(datas: Path[], ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let list = datas.map(x => new Path().ConverToEntity<Path>(x));
            return ctx.CreateBatch(list);
        });
    }
    UpdatePath(entity: Path, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new Path();
            d.ConverToEntity(entity);
            return ctx.Update(d);
        });
    }
    GetPathById(id: string) {
        let ctx = new ApiDataContext();
    }
    QueryPath(params) {
        let ctx = new ApiDataContext();
    }

    DeletePathByWorkflowId(workflowId: string, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let list = await ctx.Path.Where(x => x.workflowId == $args1, { $args1: workflowId }).ToList();
            for (let item of list) {
                await ctx.Delete<Condition>(x => x.objectId == $args1, ctx.Condition, { $args1: item.id });
            }
            return ctx.Delete<Path>(x => x.workflowId == $args1, ctx.Path, { $args1: workflowId });
        });
    }
}